package com.example.myapplication;

import com.example.myapplication.shortvideo.utils.GsonUtil;
import com.example.myapplication.util.MD5Utils;
import com.example.myapplication.videojson.BasicInfo;
import com.example.myapplication.videojson.JsonRootBean;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: rongfeng
 * @date: 2021/6/10
 * @description
 */
public class VideoJson {

    public static void main(String[] args) {
        VideoJson checkWord = new VideoJson();

        checkWord.redFile2(new File("D:\\test\\video_1.jsonl"));

        List<String> list1 = checkWord.redFile(new File("D:\\wrf\\json\\peppa_part1.txt"));
        List<VideoPath> list2 = checkWord.redFile4(new File("D:\\wrf\\json\\peppa_part1_path.txt"));

        List<VideoPath> videoPaths = new ArrayList<>();
        for (int i = 0; i < list1.size(); i++) {
            VideoPath videoPath = new VideoPath();
            videoPath.name = list1.get(i);
            for (int j = 0; j < list2.size(); j++) {
                String name = MD5Utils.createMD532("wrf_cpdc"+ videoPath.name);
                if (name.equals(list2.get(j).name)) {
                    videoPath.path = list2.get(j).path;
                }
            }
            videoPaths.add(videoPath);
        }

        String json2 = GsonUtil.toJson(videoPaths);
        checkWord.saveFile(json2, new File("D:\\wrf\\json\\peppa_video_part1.txt"));

        checkWord.redFile3(new File("D:\\wrf\\json\\peppa_video_part1.txt"));
    }


    public List<String> redFile(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String text = null;
            List<String> strings = new ArrayList<>();
            while ((text = bufferedReader.readLine()) != null) {
                strings.add(text.replace("\r", ""));
//            String de = decode(en, b);
//            System.out.println(de);

            }

            return strings;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public void redFile2(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String text = null;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("[");
            while ((text = bufferedReader.readLine()) != null) {
                if (text.contains("SubtitleInfo")) {
                    text = text + ",";
                }
                stringBuilder.append(text + "\n");
            }
            stringBuilder.delete(stringBuilder.length() - 2, stringBuilder.length());
            stringBuilder.append("]");
            text = stringBuilder.toString();

            saveFile2(text, file);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public List<VideoPath> redFile4(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String text = null;
            StringBuilder stringBuilder = new StringBuilder();
            while ((text = bufferedReader.readLine()) != null) {
                stringBuilder.append(text + "\n");
            }
            text = stringBuilder.toString();

            List<VideoPath> json = GsonUtil.parseList(text, new TypeToken<List<VideoPath>>() {
            }.getType());

            return json;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void redFile3(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String text = null;
            StringBuilder stringBuilder = new StringBuilder();
            while ((text = bufferedReader.readLine()) != null) {
                stringBuilder.append(text + "\n");
            }
            text = stringBuilder.toString();
            List<VideoPath> json = GsonUtil.parseList(text, new TypeToken<List<VideoPath>>() {
            }.getType());
            for (int i = 0; i < json.size(); i++) {
                VideoPath bean = json.get(i);
                String name = MD5Utils.createMD532("wrf_cpdc"+ bean.name);
                byte b = (byte) (name.hashCode() % 6);
                if (b == 0) {
                    b = -1;
                }
                String de = decode(bean.path, b);
                System.out.println(de);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void saveFile2(String string, File file) {
        List<JsonRootBean> json = GsonUtil.parseList(string, new TypeToken<List<JsonRootBean>>() {
        }.getType());

        List<VideoPath> videoPaths = new ArrayList<>();
        VideoPath videoPath;
        for (int i = 0; i < json.size(); i++) {
            BasicInfo bean = json.get(i).getBasicInfo();
            String name = bean.Name.replace(".mp4", "");
            byte b = (byte) (name.hashCode() % 6);
            if (b == 0) {
                b = -1;
            }
            String en = encode(bean.MediaUrl.replace("http://1258755893.vod2.myqcloud.com/24eea437vodcq1258755893/", "")
                    .replace(".mp4",""), b);
            videoPath = new VideoPath();
            videoPath.name = name;
            videoPath.path = en;
            videoPaths.add(videoPath);
//            System.out.println(en);

//            String de = decode(en, b);
//            System.out.println(de);
        }

        String json2 = GsonUtil.toJson(videoPaths);
//
        try {
            File file2 = new File("D:\\wrf\\json\\peppa_part1_path.txt");
            if (!file2.exists()) {
                file2.createNewFile();
            }
            BufferedWriter out = new BufferedWriter(new FileWriter(file2));
            out.write(json2);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static String encode(String data, byte key) {
        byte[] b = data.getBytes();
        for (int i = 0; i < b.length; i++) {
            b[i] += key;
        }
        return new String(b);
    }

    public static String decode(String data, byte key) {
        byte[] b = data.getBytes();
        for (int i = 0; i < b.length; i++) {
            b[i] -= key;
        }
        return new String(b);
    }

    public void saveFile(String string, File file) {
        try {
            if (file.exists()) {
                file.delete();
                file.createNewFile();
            }
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(string);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("保存完成 ");
    }


    public static class VideoPath implements Serializable {
        public String name;
        public String path;
    }


}
